﻿Imports KulSoft.DataAccess
Imports System.Data.OleDb
Imports System.IO
Imports System.Management
Public Class frmLogin
    'Form Login Active Các Chức Năng Của Hệ Thống Ko Được Chọn
    Private Sub frmLogin_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Me.BackColor = Color.FromArgb(194, 217, 247)
            AuthenticationForm(False)
        Catch ex As Exception
            ShowError()
        End Try
    End Sub
    'Đăng nhập Ok
    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        Try
            Cursor.Current = Cursors.WaitCursor
            lblLoginError.Text = "Vui lòng chờ..."
            lblLoginError.Visible = True
            Dim dtUserInfo As New DataTable
            Dim PasswordMD5 As String = (New Common).Encrypt(Password.Text)
            Dim query As String = "Select username,userid from Users where password='" & PasswordMD5 & "' And username='" & Username.Text.Replace("'", "") & "'"
            Dim dataAccess As New DataAccess
            Dim dbConn As New OleDb.OleDbConnection
            Try
                dataAccess.ConnectDatabase_DBAccess(dbConn)
            Catch ex As Exception
                Exit Sub
            End Try
            Dim adapter As New OleDbDataAdapter(query, dbConn)
            adapter.Fill(dtUserInfo)
            dbConn.Close()
            If dtUserInfo.Rows.Count > 0 Then
                Dim userid As Integer
                Dim username As String
                userid = Integer.Parse(dtUserInfo.Rows(0)("userid").ToString())
                username = dtUserInfo.Rows(0)("username").ToString()
                PhanQuyenMenu(userid)
                frmMain.Authentication = True
                frmMain.UserId = userid
                frmMain.UserName = username
                frmMain.lblUser.Text = "Chào mừng: " & dtUserInfo.Rows(0)("username").ToString()
                'AuthenticationForm(True)
                Me.Close()
            Else
                lblLoginError.Text = "(*) Thông tin đăng  nhập chưa đúng.Vui lòng thử lại"
                lblLoginError.Visible = True
            End If
            Cursor.Current = Cursors.Default
        Catch ex As Exception

        End Try

    End Sub

    Private Sub cmdHuyBo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdHuyBo.Click
        Try
            Application.Exit()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cmdTuyChon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTuyChon.Click
        Try
            ShowModalForm(frmLoginOptions2, False, False)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Password_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Password.KeyDown
        Try
            If e.KeyCode = Keys.Enter Then
                Cursor.Current = Cursors.WaitCursor
                lblLoginError.Text = "Vui lòng chờ..."
                lblLoginError.Visible = True
                Dim dtUserInfo As New DataTable
                Dim PasswordMD5 As String = (New Common).Encrypt(Password.Text)
                Dim query As String = "Select username,userid from Users where password='" & PasswordMD5 & "' And username='" & Username.Text.Replace("'", "") & "'"
                Dim dataAccess As New DataAccess
                Dim dbConn As New OleDb.OleDbConnection
                dataAccess.ConnectDatabase_DBAccess(dbConn)
                Dim adapter As New OleDbDataAdapter(query, dbConn)
                adapter.Fill(dtUserInfo)
                dbConn.Close()
                If dtUserInfo.Rows.Count > 0 Then
                    Dim userid As Integer
                    Dim username As String
                    userid = Integer.Parse(dtUserInfo.Rows(0)("userid").ToString())
                    username = dtUserInfo.Rows(0)("username").ToString()
                    PhanQuyenMenu(userid)
                    frmMain.Authentication = True
                    frmMain.UserId = userid
                    frmMain.UserName = username
                    frmMain.lblUser.Text = "Chào mừng: " & dtUserInfo.Rows(0)("username").ToString()
                    'AuthenticationForm(True)
                    Me.Close()
                Else
                    lblLoginError.Text = "(*) Thông tin đăng  nhập chưa đúng.Vui lòng thử lại"
                    lblLoginError.Visible = True
                End If
            End If
            Cursor.Current = Cursors.Default
        Catch ex As Exception

        End Try
    End Sub
End Class

